#include "Player.h"

//Player States
class PlayerState
{
public:
	virtual void OnEnter(Player* player) = 0;
	virtual void OnExit(Player* player) = 0;
	virtual void UpdateState(Player* player) = 0;
	virtual void DrawState(Player* player) = 0;
protected:
	PlayerState(){};
};


//States
class PlayerIdle : public PlayerState
{
public:
	void OnEnter(Player* player);
	void OnExit(Player* player);
	void UpdateState(Player* player);
	void DrawState(Player* player);
};

class PlayerWalk : public PlayerState
{
public:
	void OnEnter(Player* player);
	void OnExit(Player* player);
	void UpdateState(Player* player);
	void DrawState(Player* player);
};

class PlayerJump : public PlayerState
{
public:
	void OnEnter(Player* player);
	void OnExit(Player* player);
	void UpdateState(Player* player);
	void DrawState(Player* player);
};

//////////////////////////////////////////////////////////////////////////

//PlayerIdle
void PlayerIdle::OnEnter(Player* player)
{
	//player->SetColor(sf::Color::Red);
	player->ChangeAnim("Idle.png",0.0095f);
}

void PlayerIdle::OnExit(Player* player)
{

}

void PlayerIdle::UpdateState(Player* player)
{
	if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Right) || sf::Keyboard::IsKeyPressed(sf::Keyboard::D) ||
		sf::Keyboard::IsKeyPressed(sf::Keyboard::Left) || sf::Keyboard::IsKeyPressed(sf::Keyboard::A) &&
		player->GetVelocity().x != 0)
	{
		player->ChangeState(new PlayerWalk());
	}
	if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up) || sf::Keyboard::IsKeyPressed(sf::Keyboard::W))
	{
		player->ChangeState(new PlayerJump());
	}
}

void PlayerIdle::DrawState(Player* player)
{
	//player->Renderer;
}
//End PlayerIdle



//////////////////////////////////////////////////////////////////////////

//PlayerWalk
void PlayerWalk::OnEnter(Player* player)
{
	//player->SetColor(sf::Color::Green);
	player->ChangeAnim("Sprint.png",0.0096f);
}

void PlayerWalk::OnExit(Player* player)
{
}

#define Speed 800.0f

void PlayerWalk::UpdateState(Player* player)
{
	if (player->GetVelocity() == sf::Vector2f(0,0))
	{
		player->ChangeState(new PlayerIdle());

	}
	if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up) || sf::Keyboard::IsKeyPressed(sf::Keyboard::W) ||
		sf::Keyboard::IsKeyPressed(sf::Keyboard::Right) || sf::Keyboard::IsKeyPressed(sf::Keyboard::D) ||
		sf::Keyboard::IsKeyPressed(sf::Keyboard::Left) || sf::Keyboard::IsKeyPressed(sf::Keyboard::A))
	{
		if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up) || sf::Keyboard::IsKeyPressed(sf::Keyboard::W))
		{
			player->ChangeState(new PlayerJump());
		}
		if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Right) || sf::Keyboard::IsKeyPressed(sf::Keyboard::D))
		{
			player->ApplyForce(sf::Vector2f(Speed,0));
			
			player->Flip(false);
		}
		else if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Left) || sf::Keyboard::IsKeyPressed(sf::Keyboard::A))
		{
			player->ApplyForce(sf::Vector2f(-Speed,0));
			player->Flip(true);
		}
	}
	else
	{
		player->ApplyForce(sf::Vector2f(player->GetVelocity().x*1.3f,player->GetVelocity().y*1.3f));
	}
	player->AnimSpeed(abs(0.0096f-player->GetVelocity().x*0.00001f));
				printf("%f\n",abs(0.0096f-player->GetVelocity().x*0.00001f));

}

void PlayerWalk::DrawState(Player* player)
{
	//player->GetRenderer();
}
//End PlayerWalk


//////////////////////////////////////////////////////////////////////////

//PlayerJump
void PlayerJump::OnEnter(Player* player)
{
	player->ChangeAnim("Falling.png",0.0096f);
	player->ApplyForceImpulse(sf::Vector2f(0,-Speed*10));
}

void PlayerJump::OnExit(Player* player)
{
}


void PlayerJump::UpdateState(Player* player)
{
	if (player->GetVelocity().y == 0)
	{
		player->ChangeState(new PlayerIdle());
	}
	if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Right) || sf::Keyboard::IsKeyPressed(sf::Keyboard::D))
	{
		//player->Move(sf::Vector2f(Speed,0));
		player->ApplyForce(sf::Vector2f(Speed,0));
		player->Flip(false);
	}
	else if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Left) || sf::Keyboard::IsKeyPressed(sf::Keyboard::A))
	{
		//player->Move(sf::Vector2f(-Speed,0));
		player->ApplyForce(sf::Vector2f(-Speed,0));
		player->Flip(true);
	}
}

void PlayerJump::DrawState(Player* player)
{
	//player->GetRenderer();
}
//End PlayerJump